const { exec } = require("../db/mysql");

const addCourse = (courseData = {}) => {
  const course_title = courseData.course_title;
  const course_content = courseData.course_content;
  const tch_id = courseData.tch_id;
  const course_time = new Date().getTime();
  const sql = `insert into course (course_title,course_content,course_time,tch_id) 
    values('${course_title}','${course_content}','${course_time}','${tch_id}');`;
  return exec(sql).then((insertData) => {
    if (insertData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const getCourseList = (id) => {
  const sql = `select course.*,teacher.tch_name from course,teacher where course.tch_id=teacher.tch_id and course.tch_id='${id}'`;
  return exec(sql);
};
const getHomeCourse = () => {
  const sql = `select course.*,teacher.tch_name from course,teacher where course.tch_id=teacher.tch_id `;
  return exec(sql);
};
const delCourse = (id) => {
  const sql = `delete from course where course_id=${id}`;
  return exec(sql).then((delData) => {
    if (delData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};
const updateCourse = (id, courseData = {}) => {
  let course_title = courseData.course_title;
  let course_content = courseData.course_content;
  let course_time = new Date().getTime();

  const sql = `update course set course_title='${course_title}',course_content='${course_content}' ,
  course_time='${course_time}' where course_id=${id}`;
  return exec(sql).then((updateData) => {
    if (updateData.affectedRows > 0) {
      return true;
    }
    return false;
  });
};

module.exports = {
  addCourse,
  getCourseList,
  getHomeCourse,
  delCourse,
  updateCourse,
};
